package controller.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import factory.UserFactory;

@WebServlet("/validata")
public class ValidataUserNameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserDao userDao;
		try {
			userDao = UserFactory.getInstance("userdaoimpl", UserDao.class);
			String uname = request.getParameter("uname");
			System.out.println(uname);
			PrintWriter out = response.getWriter();
			if(userDao.getAppointUser(uname) != null) {
				out.println("<font color='red'>已注册</font>");
				return ;
			}else if(uname != null && !uname.trim().equals("")) {
				out.println("<font color='green'>可用</font>");
			}else {
				out.println("<font color='red'>不为空！</font>");
				return ;
			}
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}